package com.lin.cms.web.statistic;

import static com.lin.cms.web.statistic.CmsStatistic.THISMONTH;
import static com.lin.cms.web.statistic.CmsStatistic.THISWEEK;
import static com.lin.cms.web.statistic.CmsStatistic.THISYEAR;
import static com.lin.cms.web.statistic.CmsStatistic.TODAY;
import static com.lin.cms.web.statistic.CmsStatistic.YESTERDAY;
import static java.util.Calendar.DATE;
import static java.util.Calendar.DAY_OF_WEEK;
import static java.util.Calendar.MONTH;
import static java.util.Calendar.YEAR;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lin.cms.dao.SiteFlowMapper;
import com.lin.cms.web.statistic.CmsStatistic.TimeRange;

@Service
@Transactional(readOnly = true)
public class CmsStatisticSvcImpl implements CmsStatisticSvc {

	public Map<String, List<CmsStatistic>> getWelcomeSiteFlowData(Integer siteId) {
		Map<String, List<CmsStatistic>> map = new HashMap<String, List<CmsStatistic>>();
		map.put("today", getListByTimeRange(siteId, getTimeRange(TODAY)));
		map.put("yesterday",getListByTimeRange(siteId, getTimeRange(YESTERDAY)));
		map.put("thisweek", getListByTimeRange(siteId, getTimeRange(THISWEEK)));
		map.put("thismonth",getListByTimeRange(siteId, getTimeRange(THISMONTH)));
		map.put("total",getListByTimeRange(siteId, getTimeRange(-1)));
		return map;
	}
	
	private List<CmsStatistic> getListByTimeRange(Integer siteId,
			TimeRange timeRange) {
		List<CmsStatistic> list = new ArrayList<CmsStatistic>();
		list.add(new CmsStatistic(getPvCountByTimeRange(siteId, timeRange)));
		list.add(new CmsStatistic(getUniqueIpCountByTimeRange(siteId, timeRange)));
		list.add(new CmsStatistic(getUniqueVisitorCountByTimeRange(siteId, timeRange)));
		return list;
	}
	
	private long getPvCountByTimeRange(Integer siteId, TimeRange timeRange) {
		return cmsStatisticDao.getPvCountByTimeRange(siteId, timeRange);
	}
	
	private long getUniqueIpCountByTimeRange(Integer siteId, TimeRange timeRange) {
		return cmsStatisticDao.getUniqueIpCountByTimeRange(siteId, timeRange);
	}

	private long getUniqueVisitorCountByTimeRange(Integer siteId, TimeRange timeRange) {
		return cmsStatisticDao.getUniqueVisitorCountByTimeRange(siteId, timeRange);
	}
	
	private int getYear(Calendar calendar) {
		return calendar.get(YEAR);
	}
	private int getMonth(Calendar calendar) {
		return calendar.get(MONTH);
	}

	private int getDay(Calendar calendar) {
		return calendar.get(DATE);
	}
	private Calendar clearTime(Calendar calendar) {
		return new GregorianCalendar(getYear(calendar), getMonth(calendar),
				getDay(calendar));
	}
	
	private TimeRange getTimeRange(int type){
		return getTimeRange(type, new GregorianCalendar());
	}
	
	private void flush(Calendar calendar) {
		calendar.getTime();
	}
	
	// 获取今日、昨日、本周、本月时间范围
		private TimeRange getTimeRange(int type, Calendar calendar) {
			calendar = clearTime(calendar);
			Date begin, end;
			switch (type) {
			case TODAY: {
				begin = calendar.getTime();
				calendar.add(DATE, 1);
				end = calendar.getTime();
				return TimeRange.getInstance(begin, end);
			}
			case YESTERDAY: {
				calendar.add(DATE, -1);
				begin = calendar.getTime();
				calendar.add(DATE, 1);
				end = calendar.getTime();
				return TimeRange.getInstance(begin, end);
			}
			case THISWEEK: {
				flush(calendar);
				calendar.set(DAY_OF_WEEK, 1);
				begin = calendar.getTime();
				calendar.add(DAY_OF_WEEK, 7);
				end = calendar.getTime();
				return TimeRange.getInstance(begin, end);
			}
			case THISMONTH: {
				int month = calendar.get(MONTH);
				calendar.set(DATE, 1);
				begin = calendar.getTime();
				calendar.set(MONTH, month + 1);
				end = calendar.getTime();
				return TimeRange.getInstance(begin, end);
			}
			case THISYEAR:{
				int year = calendar.get(YEAR);
				calendar.set(MONTH, 0);
				calendar.set(DATE, 1);
				begin = calendar.getTime();
				calendar.set(YEAR, year + 1);
				end = calendar.getTime();
				return TimeRange.getInstance(begin, end);
			}
			}
			return null;
		}
	
		@Autowired
		private SiteFlowMapper cmsStatisticDao;
}
